package utils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.List;

public class ExcelUtil implements Serializable {

	private static final long serialVersionUID = 1L;
		
	public static InputStream excel(List<String> fieldHead ,List<List<String>> fieldData) throws Exception{
		
		StringBuffer sb = new StringBuffer();
		sb.append("<html>");
		sb.append("<head>");
		sb.append("<meta http-equiv=Content-Type content='text/html;charset=utf-8'>");
		sb.append("</head><body>");
		sb.append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
		sb.append("<tr style='font-weight: bold; white-space: nowrap;'>");
		for(String head:fieldHead){
			sb.append("<td  style='vnd.ms-excel.numberformat:@'>"+head+"</td>");
		}
		sb.append("</tr>");

		for (List<String> data : fieldData) {
			sb.append("<tr>");
			for(String item:data){
				if(StringUtil.isEmpty(item)){
					sb.append("<td  style='vnd.ms-excel.numberformat:@'></td>");
				}else{
					sb.append("<td  style='vnd.ms-excel.numberformat:@'>" + item + "</td>");
				}
			}
			sb.append("</tr>");
		}
		sb.append("</table>");
		sb.append("</body></html>");
		return new ByteArrayInputStream(sb.toString().getBytes("UTF-8"), 0, sb.toString().getBytes("UTF-8").length);
	}
}
